<?php
/**
 * tpshop
 * ============================================================================
 * * 版权所有 2015-2027 深圳搜豹网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.tp-shop.cn
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * 采用TP5助手函数可实现单字母函数M D U等,也可db::name方式,可双向兼容
 * ============================================================================
 * 2015-11-21
 */
namespace app\mobile\controller;

use app\common\logic\CartLogic;
use app\common\logic\MessageLogic;
use app\common\logic\UsersLogic;
use app\common\logic\OrderLogic;
use think\Page;
use think\Request;
use think\Verify;
use think\db;

class Distribution extends MobileBase
{

    public $user_id = 0;
    public $user = array();

    public function _initialize()
    {
        parent::_initialize();
        if (session('?user')) {
            $user = session('user');
            $user = M('users')->where("user_id", $user['user_id'])->find();
            session('user', $user);  //覆盖session 中的 user
            $this->user = $user;
            $this->user_id = $user['user_id'];
            $this->assign('user', $user); //存储用户信息
            $this->assign('user_id', $this->user_id);
        } else {
            header("location:" . U('User/login'));
            exit;
        }
      
    }
    /**
     * 分销首页
     * @return type
     */
    public function index(){
         $condition['user_id'] = $this->user_id;
         $money = M('rebate_log')->query("select sum(money) as money from tp_rebate_log where user_id =".$condition['user_id']);
         $to_log = M('rebate_log')->where(" user_id =".$condition['user_id'])->select();
         $to_money = 0;
         foreach($to_log as $v){
             $ctime = date('Y-m-d',$v['create_time']);
             if($ctime == date('Y-m-d')){
                 $to_money += $v['money'];
             }
         }
         $m = $money[0]['money']>0?$money[0]['money']:0;
         $this->assign('money', $m);// 赋值分页输出
         //统计1下线人数
         $count = M('users')->field('user_id')->where('first_leader='.$this->user_id)->count();
         $first = M('users')->field('user_id,first_leader')->where('first_leader='.$this->user_id)->select();
         $sec_num = array();
         foreach($first as $v){
            $sec_num[] = M('users')->field('user_id')->where('first_leader='.$v['user_id'])->count();
         }
         $sec_sum = 0;
         foreach($sec_num as $v){
             $sec_sum += $v;
         }
         $count = $count>0?$count:0;
         $first_sec_sum = $count+$sec_sum;
         $this->assign('to_money', $to_money);// 赋值分页输出
         $this->assign('count', $first_sec_sum);// 赋值分页输出
         return $this->fetch();
    }
//    /**
//     * 分销订单
//     * @return type
//     */
//    public function order_list(){
//        $user_id = $this->user_id;
//        $status = I('get.status');
//        $result = $this->get_order_list($user_id , $status); //获取分销订单列表
//        $this->assign('rebate_log', $result['result']);
//         if ($_GET['is_ajax']) {
//            return $this->fetch('ajax_order_list');
//            exit;
//        }
//         return $this->fetch();
//    }
//

//    public function get_order_list($user_id, $status = 3)
//    {
//        if ($status == 1) {
//            //未付款
//            $comment_list = M('rebate_log')->alias('c')
//->field('c.*,og.goods_id,og.goods_name,l.level_name,o.total_amount')
//->join('__ORDER__ o', 'o.order_id = c.order_id')
//->join('__USER_LEVEL__ l', 'l.level_id = c.level')
//->join('__ORDER_GOODS__ og','c.order_id = og.order_id')
//->where("c.user_id=$user_id and c.status=0")
//->select();
//            $commented_count = M('rebate_log')->alias('c')->field('c.*,og.goods_id,og.goods_name,l.level_name,o.total_amount')->join('__ORDER__ o', 'o.order_id = c.order_id')->join('__USER_LEVEL__ l', 'l.level_id = c.level')->join('__ORDER_GOODS__ og','c.order_id = og.order_id')->where("c.user_id=$user_id and c.status=0")->count();
//            $page = new \think\Page($commented_count, 2);
//        } else if ($status == 2) {
//             $comment_list = M('rebate_log')->alias('c')->field('c.*,og.goods_id,og.goods_name,l.level_name,o.total_amount')->join('__ORDER__ o', 'o.order_id = c.order_id')->join('__USER_LEVEL__ l', 'l.level_id = c.level')->join('__ORDER_GOODS__ og','c.order_id = og.order_id')->where("c.user_id=$user_id and c.status=1")->select();
//            $commented_count = M('rebate_log')->alias('c')->field('c.*,og.goods_id,og.goods_name,l.level_name,o.total_amount')->join('__ORDER__ o', 'o.order_id = c.order_id')->join('__USER_LEVEL__ l', 'l.level_id = c.level')->join('__ORDER_GOODS__ og','c.order_id = og.order_id')->where("c.user_id=$user_id and c.status=1")->count();
//            $page = new \think\Page($commented_count, 2);
//        }else if($status==3){
//            //已完成
//            $comment_list = M('rebate_log')->alias('c')->field('c.*,og.goods_id,og.goods_name,l.level_name,o.total_amount')->join('__ORDER__ o', 'o.order_id = c.order_id')->join('__USER_LEVEL__ l', 'l.level_id = c.level')->join('__ORDER_GOODS__ og','c.order_id = og.order_id')->where("c.user_id=$user_id and c.status=3")->select();
//            $commented_count = M('rebate_log')->alias('c')->field('c.*,og.goods_id,og.goods_name,l.level_name,o.total_amount')->join('__ORDER__ o', 'o.order_id = c.order_id')->join('__USER_LEVEL__ l', 'l.level_id = c.level')->join('__ORDER_GOODS__ og','c.order_id = og.order_id')->where("c.user_id=$user_id and c.status=3")->count();
//            $page = new \think\Page($commented_count, 2);
//
//        }else{
//             //全部
//            $comment_list = M('rebate_log')->alias('c')->field('c.*,og.goods_id,og.goods_name,l.level_name,o.total_amount')->join('__ORDER__ o', 'o.order_id = c.order_id')->join('__USER_LEVEL__ l', 'l.level_id = c.level')->join('__ORDER_GOODS__ og','c.order_id = og.order_id')->where("c.user_id=$user_id")->select();
//            $commented_count = M('rebate_log')->alias('c')->field('c.*,og.goods_id,og.goods_name,l.level_name,o.total_amount')->join('__ORDER__ o', 'o.order_id = c.order_id')->join('__USER_LEVEL__ l', 'l.level_id = c.level')->join('__ORDER_GOODS__ og','c.order_id = og.order_id')->where("c.user_id=$user_id")->count();
//            $page = new \think\Page($commented_count, 2);
//        }
//
//        $show = $page->show();
//        $return['result'] = $comment_list;
//        $return['show'] = $show; //分页
//        $return['page'] = $page; //分页
//        return $return;
//    }




////////////**************新增start****************///////////////////
    /**
     * 分销订单
     * @return type
     */
    public function order_list(){
        $user_id = $this->user_id;
        $status = I('get.status');
        $result = $this->get_order_list($user_id,$status); //获取分销订单列表
        $this->assign('rebate_log', $result['result']);
         if ($_GET['is_ajax']) {
            return $this->fetch('ajax_order_list');
            exit;
        }
         return $this->fetch();
    }
    /**
     * @param $user_id   获取分销订单列表
     * @param int $status
     * @return mixed
     */
    public function get_order_list($user_id,$status){
        if ($status == 1) {
            //未付款
            $comment_list = M('rebate_log')->alias('c')
                ->field('c.*,og.goods_id,og.goods_name,l.type_name,o.total_amount')
                ->join('__ORDER__ o', 'o.order_id = c.order_id')
                ->join('__USER_TYPE__ l', 'l.type_id = c.type')
                ->join('__ORDER_GOODS__ og','c.order_id = og.order_id')
                ->where("c.user_id=$user_id and c.status=0 and (og.goods_id = 144 or og.goods_id = 145)")
                ->select();
            $commented_count = M('rebate_log')->alias('c')
                ->field('c.*,og.goods_id,og.goods_name,l.type_name,o.total_amount')
                ->join('__ORDER__ o', 'o.order_id = c.order_id')
                ->join('__USER_TYPE__ l', 'l.type_id = c.type')
                ->join('__ORDER_GOODS__ og','c.order_id = og.order_id')
                ->where("c.user_id=$user_id and c.status=0 and (og.goods_id = 144 or og.goods_id = 145)")
                ->count();
            $page = new \think\Page($commented_count, 2);
        } else if ($status == 2) {
            //已付款
             $comment_list = M('rebate_log')->alias('c')
                 ->field('c.*,og.goods_id,og.goods_name,l.type_name,o.total_amount')
                 ->join('__ORDER__ o', 'o.order_id = c.order_id')
                 ->join('__USER_TYPE__ l', 'l.type_id = c.type')
                 ->join('__ORDER_GOODS__ og','c.order_id = og.order_id')
                 ->where("c.user_id=$user_id and c.status=1 and (og.goods_id = 144 or og.goods_id = 145)")
                 ->select();
            $commented_count = M('rebate_log')->alias('c')
                ->field('c.*,og.goods_id,og.goods_name,l.type_name,o.total_amount')
                ->join('__ORDER__ o', 'o.order_id = c.order_id')
                ->join('__USER_TYPE__ l', 'l.type_id = c.type')
                ->join('__ORDER_GOODS__ og','c.order_id = og.order_id')
                ->where("c.user_id=$user_id and c.status=1 and (og.goods_id = 144 or og.goods_id = 145)")
                ->count();
            $page = new \think\Page($commented_count, 2);
        }else if($status==3){
            //已完成
            $comment_list = M('rebate_log')->alias('c')
                ->field('c.*,og.goods_id,og.goods_name,l.type_name,o.total_amount')
                ->join('__ORDER__ o', 'o.order_id = c.order_id')
                ->join('__USER_TYPE__ l', 'l.type_id = c.type')
                ->join('__ORDER_GOODS__ og','c.order_id = og.order_id')
                ->where("c.user_id=$user_id and c.status=3 and (og.goods_id = 144 or og.goods_id = 145)")
                ->select();
            $commented_count = M('rebate_log')->alias('c')
                ->field('c.*,og.goods_id,og.goods_name,l.type_name,o.total_amount')
                ->join('__ORDER__ o', 'o.order_id = c.order_id')
                ->join('__USER_TYPE__ l', 'l.type_id = c.type')
                ->join('__ORDER_GOODS__ og','c.order_id = og.order_id')
                ->where("c.user_id=$user_id and c.status=3 and (og.goods_id = 144 or og.goods_id = 145)")
                ->count();
            $page = new \think\Page($commented_count, 2);

        }else{
             //全部
            $comment_list = M('rebate_log')->alias('c')
                ->field('c.*,og.goods_id,og.goods_name,l.type_name,o.total_amount')
                ->join('__ORDER__ o', 'o.order_id = c.order_id')
                ->join('__USER_TYPE__ l', 'l.type_id = c.type')
                ->join('__ORDER_GOODS__ og','c.order_id = og.order_id')
                ->where("c.user_id=$user_id and (og.goods_id = 144 or og.goods_id = 145)")
                ->select();
            $commented_count = M('rebate_log')->alias('c')
                ->field('c.*,og.goods_id,og.goods_name,l.type_name,o.total_amount')
                ->join('__ORDER__ o', 'o.order_id = c.order_id')
                ->join('__USER_TYPE__ l', 'l.type_id = c.type')
                ->join('__ORDER_GOODS__ og','c.order_id = og.order_id')
                ->where("c.user_id=$user_id and (og.goods_id = 144 or og.goods_id = 145)")
                ->count();

            $page = new \think\Page($commented_count, 2);
        }
        $show = $page->show();
        $return['result'] = $comment_list;
        $return['show'] = $show; //分页
        $return['page'] = $page; //分页
        return $return;
    }

    ////////////**************新增end****************///////////////////




    /**
     * 我的团队
     * @return type
     */
    public function lower_list(){
         return $this->fetch();
    }
    /**
     * 分销排行
     * @return type
     */
    public function distribut_money(){
        // SELECT user_id,sum(money)as money FROM `tp_rebate_log` group by user_id ORDER BY money desc
        $paihang1 = M('rebate_log')->field('user_id,sum(money) as money')->group('user_id')->ORDER('money desc')->limit('0,0')->select();
        $users1 = M('Users')->where(['user_id'=>$paihang1[0]['user_id']])->find();
        $paihang2 = M('rebate_log')->field('user_id,sum(money) as money')->group('user_id')->ORDER('money desc')->limit('1,1')->select();
        $users2 = M('Users')->where(['user_id'=>$paihang2[0]['user_id']])->find();
        $paihang3 = M('rebate_log')->field('user_id,sum(money) as money')->group('user_id')->ORDER('money desc')->limit('2,2')->select();
        $users3 = M('Users')->where(['user_id'=>$paihang3[0]['user_id']])->find();


        $paiming = 4;
        $paihangs = M('rebate_log')->field('user_id,sum(money) as money')->group('user_id')->ORDER('money desc')->limit('3,20')->select();
         foreach ($paihangs as $key => $value) {
             $users = M('Users')->where(['user_id'=>$value['user_id']])->find();

             $paihang[$key]['money'] = $value['money'];
             $paihang[$key]['user_id'] = $value['user_id'];
             $paihang[$key]['nickname'] = $users['nickname'];
             $paihang[$key]['head_pic'] = $users['head_pic'];
             $paihang[$key]['paiming'] = $paiming++;
             // if(!empty($users)){
             //    $user[] = $users;
             // }
         }

         $this->assign('paihang1', $paihang1); //存储用户信息
         $this->assign('users1', $users1); //存储用户信息
         $this->assign('paihang2', $paihang2); //存储用户信息
         $this->assign('users2', $users2); //存储用户信息
         $this->assign('paihang3', $paihang3); //存储用户信息
         $this->assign('users3', $users3); //存储用户信息
         $this->assign('paihang', $paihang); //存储用户信息
         return $this->fetch();
    }

    
}